#!/usr/bin/env bash
# set -e
# pathwork=~/.local/share/fcitx5/rime
dictmain=aniu.dict.yaml
dictspz=aniu_spz.dict.yaml
dictpri=aniu_pri.dict.yaml

cd ~/.local/share/fcitx5/rime

function chaxun(){
	# 以编码查询
	cd ~/.local/share/fcitx5/rime
	if echo $1 |grep -q ",";then
		str1=`echo $1|cut -d "," -f1`
		echo -e "\n\e[33m列出形如\e[0m$str1\e[33m的单字编码：\n————————————————————————————————————\e[0m"
        grep -Pnh "^.\t$str1\b" $dictmain $dictspz  #|sed -r -e "/\t5/d"
		echo -e "\e[33m————————————————————————————————————\e[0m"
		unset str1
		return
	elif echo $1 |grep -q "[a-z]"; then
		echo -e "\n\e[33m列出形如\e[0m$1\e[33m的编码：\n————————————————————————————————————\e[0m"
		grep -Pn "\b$1\b" $dictmain $dictspz $dictpri|sed -r "/\t3/d;s/.dict.yaml//g"
		echo -e "\e[33m————————————————————————————————————\e[0m"
		return
	fi
	# 以汉字查询编码
	echo -e "\n\e[33m列出\e[0m$1\e[33m的编码：\n————————————————————————————————————\e[0m"
	grep -Pn  "^$1\t" $dictmain $dictspz $dictpri|sed -r "s/.dict.yaml//g"

	echo -e "\e[33m————————————————————————————————————\e[0m"
#     python -c "print('\033[1;34m=\033[0m'*10)" # 重复打印“=”
}

# 定义五笔练习函数
function lianxi() {
    # 进入工作目录
    cd $HOME/study/gitee/collection/五笔自定义词库
    pathlianxi=/tmp/tmp_lianxi`date +%s`.wb
    touch $pathlianxi
    echo -e "请选择练习项目：
    输入 1 选择：一级简码
    输入 2 选择：二级简码(1)
    输入 3 选择：二级简码(2)
    输入 4 选择：常用前500字
    输入 5 选择：常用中500字
    输入 6 选择：常用后500字
    输入 7 选择：词语练习"
    read -r -p "请输入您的选择(默认为4)：" input
    case $input in
        1)
            for ((i=1; i<=10; i++))
            do
                shuf -rn 30 -e `sed -n "1,2p" 一简` |xargs|sed "s/ //g;s/$/\n/g" >>$pathlianxi
            done;;
        2)
            awk -F "\t" '$3==100{print $1}' 二简|shuf -rn 600 |xargs|sed "s/ //g" >>$pathlianxi
            ;;
        3)
            awk -F "\t" '$3==90{print $1}' 二简|shuf -rn 600 |xargs|sed "s/ //g" >>$pathlianxi
            ;;
        4|"")
            for ((i=1; i<=20; i++))
            do
                shuf -rn 30 -e `sed -n "1p" 常用字` |xargs|sed "s/ //g;s/$/\n/g" >>$pathlianxi
            done;;
        5)
            for ((i=1; i<=20; i++))
            do
                shuf -rn 30 -e `sed -n "2p" 常用字` |xargs|sed "s/ //g;s/$/\n/g" >>$pathlianxi
            done;;
        6)
            for ((i=1; i<=20; i++))
            do
                shuf -rn 30 -e `sed -n "3p" 常用字` |xargs|sed "s/ //g;s/$/\n/g" >>$pathlianxi
            done;;
        7)
            for ((i=1; i<=30; i++))
            do
                shuf -r -n 20 词语|xargs|sed "s/$/\n/g" >>$pathlianxi
            done;;
        *)
            echo "您输入的数字不对。"
            exit 1;;
    esac

    kate -l 2 --tempfile $pathlianxi &
    unset pathlianxi
}

case "$1" in
	-h|--help|"")
		h h ri;;
	-d)
		echo -e "第\e[34m$2\e[0m行的编码是：\e[34m"`sed -n "$2 p" $dictmain`"\e[0m"
        sed -i "$2 d" $dictmain
        echo -e "已将其\e[91m删除\e[0m";;
	-D)
		if sed -i "/^$2\t$3\b/d" $dictmain;then
			echo -e "已将该编码\e[91m删除\e[0m"
		fi;;
	-c)
        sed -i "$2 c $3\t$4\t$5" $dictmain
        echo -e "已将第\e[34m$2\e[0m行的内容替换为编码：\e[34m$3\t$4\t$5\e[0m"
        sed -n "$2 p" $dictmain;;
	-C)
		if sed -i "/^$2\t$3\b/c $4\t$5\t$6" $dictmain;then
			echo -e "已将第一组编码替换为：\e[34m$4\t$5\t$6\e[0m"
            chaxun $4
		fi;;
	-w)
		if sed -ir "$2 s/[0-9]*$/$3/g" $dictmain; then
			echo -e "已将编码权重更改为：\e[34m$3\e[0m"
            sed -n "$2 p" $dictmain
		fi;;
	-i)
			sed -i "$2 i $3\t$4\t$5" $dictmain
			echo -e "已在$2处插入新的编码：\e[34m$3\t$4\t$5\e[0m"
		;;
  -p)
	    echo -e "$2\t$3\t$4" >> $dictpri
	    echo -e "已向pri词库追加新的编码：\e[34m$2\t$3\t$4\e[0m";;
  -v)
      fiel1=($(fd -t f --max-depth=1 aniu|fzf -q $2))
      [[ -n "$fiel1" ]] && vim "${fiel1[@]}"
      unset fiel1;;
	--out)
        echo -e "请选择输出的项目：
        输入 \e[33m1\e[0m 选择：输出一份适合百度手机导入的码表
        输入 \e[33m1 shouji\e[0m ：输出码表之后发送一份邮件
        输入 \e[33m2\e[0m 选择：输出一份适合练习的二级简码表
        "

        read -r -t 10 -p "请输入您的选择：" input1 input2
        case $input1 in
            1)
                bdsj=~/study/gitee/collection/五笔自定义词库/手机.txt
        		    sed -n "/\b3$/d;/\t/p" $dictmain > $bdsj
        		    sed -n "/print/d;/\t/p" $dictpri >> $bdsj
                sort -k 2,2 -k 3,3nr $bdsj > /tmp/bdsj.txt
                awk -F "\t" '{print $2 " " $1}' /tmp/bdsj.txt >$bdsj
                if [[ $input2 == "shouji" ]]; then
                    echo "邮件包含一份\"五笔码表\"，请查收。" |mail -s "百度输入法五笔方案" -a $bdsj aniu
                fi;;
            2)
                ejjm=~/study/gitee/collection/五笔自定义词库/二简
                sed -nr "/^.\t..\t[^53]/p" $dictmain |sort -k3 >$ejjm;;
            *)
                echo "什么也没发生";;
        esac
        unset bdsj input1 input2 ejjm;;
	-l)
		lianxi;;
	*)
		chaxun $@
		;;
esac
unset dictmain dictspz
